package com.atguigu.gmall.seckill.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.result.ResultCodeEnum;
import com.atguigu.gmall.seckill.biz.SeckillBizService;
import com.atguigu.gmall.seckill.vo.SeckillOrderSubmitVo;
import org.bouncycastle.cms.bc.BcKeyTransRecipientInfoGenerator;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author lhh
 * @ClassName SeckillApiController
 * @date 2022 2022/12/29
 */
@RequestMapping("api/activity/seckill/auth")
@RestController
public class SeckillApiController {

    @Resource
    SeckillBizService seckillBizService;

    @GetMapping("getSeckillSkuIdStr/{skuId}")
    public Result getSeckillSkuIdStr(@PathVariable("skuId") Long skuId) {
        String code = seckillBizService.generateSeckillCode(skuId);
        return Result.ok(code);
    }

    //seckillOrder/50?skuIdStr=b61a1467fdb4ca5af412af6821e5bafa
    @PostMapping("seckillOrder/{skuId}")
    public Result seckillOrder(@PathVariable("skuId") Long skuId, @RequestParam("skuIdStr") String code) {
        //秒杀下单
        seckillBizService.seckillOrder(skuId, code);
        return Result.ok();
    }

    /**
     * 检查秒杀单
     *
     * @param skuId
     * @return
     */
    @GetMapping("checkOrder/{skuId}")
    public Result checkOrder(@PathVariable("skuId") Long skuId) {

        //检查秒杀单状态
        ResultCodeEnum codeEnum = seckillBizService.checkOrder(skuId);

        return Result.build("", codeEnum);
    }

    /**
     * 提交订单
     *
     * @param submitVo
     * @return
     */
    @PostMapping("/submitOrder")
    public Result submitOrder(@RequestBody SeckillOrderSubmitVo submitVo) {
        Long orderId = seckillBizService.submitOrder(submitVo);
        return Result.ok(orderId.toString());
    }
}
